do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
	
drop if prize10M > 5

gen tot_asset = pers_asset + spouse_asset
	
gen anybaby_pre1 = totbaby_pre1 > 0
gen single_pre1 = married_pre1 == 0
gen singlebaby_pre1 = totbaby_pre1 > 0 if married_pre1 == 0
gen couplebaby_pre1 = totbaby_pre1 > 0 if married_pre1 == 1
gen nobabyold_pre1 = win_age >= 30 if totbaby_pre1 == 0
gen withbabyold_pre1 = win_age >= 30 if totbaby_pre1 > 0
gen middleage_pre1 = inrange(win_age,30,44)

replace norm_year = norm_year + 3

foreach grp in anybaby single couplebaby{
	foreach x in currentxprizexpre3 currentxprizexpre2 currentxprizexpost0 currentxprizexpost1 currentxprizexpost2 currentxprizexpost3 currentxprizexpost4 currentxprizexpost5 currentxprizexpost6{
		gen `grp'x`x' = 0
		replace `grp'x`x' = `x' if `grp'_pre1 == 1
	}	
}
	
cap rm "$table/temp/Tab5_Interaction.txt"
cap rm "$table/temp/Tab5_Interaction.xls"

foreach y in totbaby { 
	foreach grp in anybaby single couplebaby{
		
		global control1 "i.pers_gender#i.`grp'_pre1 i.houhld_resid_cd#i.`grp'_pre1 c.pers_income_pre1#i.`grp'_pre1 c.pers_asset_pre1#i.`grp'_pre1 c.pers_earnings_pre1#i.`grp'_pre1 i.pers_labor_pre1#i.`grp'_pre1 i.married_pre1#i.`grp'_pre1"
		global control2 "c.totbaby_pre1#i.`grp'_pre1"
		global control3 "c.tot_redeem_lotto#i.`grp'_pre1 c.tot_redeem_receipt#i.`grp'_pre1"

		reghdfe `y' currentxprizexp* `grp'xcurrentxprizexp* c.prize10M#i.`grp'_pre1 c.prize10M#i.norm#i.`grp'_pre1 c.prize10M#i.current#i.`grp'_pre1 $control1 $control2 $control3, a(norm#current#`grp'_pre1 year#`grp'_pre1 age#`grp'_pre1) cl(houseid)
			
		outreg2 using "$table/temp/Tab5_Interaction.xls" , append bd(3) sd(3) nocon keep(*currentxprize*) ctitle(`grp')

	}
}

import delimited using "$table/temp/Tab5_Interaction.txt", clear

replace v1 = substr(v1,strpos(v1,"current"),.) if strpos(v1,"current") != 0
replace v1 = "SubGrp#" + v1 if inrange(_n,22,111) & v1 != ""

forv i = 3(1)5{
	forv j = 1(1)18{
		loc k = `j'+21
		loc m = (`i'-2)*18
			replace v`i' = v`i'[_n+`m'] in `k'
	}
}

drop in 40/93


replace v2 = "w/ Child" in 2
replace v3 = "Single" in 2
replace v4 = "Couple w/ Child" in 2

forv i = 1(1)4{
	rename v`i' vv`i'
}

drop in 4/21

save "$table/temp/Tab5-I.dta", replace


export excel "$table/Tables.xlsx", sheet("Tab5-I") sheetreplace
